package controller;

import java.io.IOException;
import java.sql.SQLException;
import java.util.ArrayList;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import manager.LeaveManager;
import data.dao.LeaveDAOException;
import data.dao.daoImplementation.NotFoundException;
import data.dto.LeaveDTO;
import data.dto.UserDTO;
import data.searchdto.LeaveSearchDTO;

/**
 * Servlet implementation class ManageStaffLeaveServlet
 */
@WebServlet("/ManageStaffLeaveServlet")
public class ManageStaffLeaveServlet extends HttpServlet {
	private static final long serialVersionUID = 1L;

	/**
	 * @see HttpServlet#HttpServlet()
	 */
	public ManageStaffLeaveServlet() {
		super();
		// TODO Auto-generated constructor stub
	}

	/**
	 * @see HttpServlet#doGet(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doGet(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	/**
	 * @see HttpServlet#doPost(HttpServletRequest request, HttpServletResponse
	 *      response)
	 */
	protected void doPost(HttpServletRequest request,
			HttpServletResponse response) throws ServletException, IOException {
		// TODO Auto-generated method stub
		try {
			processRequest(request, response);
		} catch (LeaveDAOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	private void processRequest(HttpServletRequest request,
			HttpServletResponse response) throws LeaveDAOException,
			ServletException, IOException, SQLException {
		String page = "";
		UserDTO user = util.CurrentUser.getCurrentUser(request.getSession());

		// check user
		if (user == null) {
			page = util.Constants.homePage;
		} else if (util.CurrentUser.isAdmin(user)) {
			page = util.Constants.homePage;
		} else {

			LeaveManager leavemgr = new LeaveManager();
			LeaveSearchDTO choose = new LeaveSearchDTO();

			Short type = null;
			if (null != request.getParameter("status"))
				type = Short.parseShort(request.getParameter("status"));
			else
				type = Short.parseShort("10");

			if (!(type == 0 || type == 10)) {
				// if(choose.getStatus() != 0)
				// {
				choose.setStatus(Short.parseShort(request
						.getParameter("status")));
				ArrayList<LeaveDTO> leavedata = leavemgr.findLeave(choose);
				request.setAttribute("status", leavedata);
				// RequestDispatcher red=
				// request.getRequestDispatcher("manageStaffLeave.jsp");
				// red.forward(request, response);
			}

			else {
				ArrayList<LeaveDTO> leavedata = leavemgr.findAllLeave();
				request.setAttribute("status", leavedata);
				// RequestDispatcher red=
				// request.getRequestDispatcher("manageStaffLeave.jsp");
				// red.forward(request, response);
			}
			page = "/staff/manageStaffLeave.jsp";
		}

		RequestDispatcher red = request
				.getRequestDispatcher(page);
		red.forward(request, response);

	}

}
